$(document).ready(function(){


	//$("#add-messages-wrap").hide();

	//$("#btn-messages").click(function(){
		//reset form
	//	$('select[name=totype]').val('all');
		//$('#totype-list-recipients').hide();
		//$('input[name=m-subject]').val('Subject');
	//	$('textarea[name=m-message]').val('');
		//$('input[name=file_upload]').val('');
		//$("#add-messages-wrap").fadeIn("slow");
		//$(".error-r").css('margin-top','-999px');
	//});

	$('#youtube-in-wrap').hide()
	$('#youtube-link').click(function(){
		$('#youtube-in-wrap').slideToggle();
		return false;
	})

	$('#cancelButton').click(function(){
		$("#submitBtn-wrapper").fadeIn();
		$("#camera").hide();
	});

	$("#capture-btn").click(function(){
		$("#submitBtn-wrapper").fadeOut();
		$("#camera").focus();
		$("#camera").slideToggle();
	});

	$('#totype').change(function(){
		value = $(this).val()
		switch(value){
			case 'all':
				$('#totype-desc').html('Your message will be sent to all your recipients.');
				$('#totype-list-recipients').fadeOut('slow');
				break
			case 'specific':
				$('#totype-desc').html('Your message will be sent to selected recipients.');
				$('#totype-list-recipients').fadeIn('slow');
				break;
			case 'public':
				$('#totype-desc').html('Your message will be displayed in public.')
				$('#totype-list-recipients').fadeOut('slow');
				break
		}
	});

	$("#add-messages-wrap input[name=m-subject]").focus(function(){ $(this).val('');
		 switch($(this).attr('id')){
			 case 'm-to': $(this).focusout(function(){
				if($(this).val()=='') $(this).val('To');
			 }); break;

			 case 'm-subject': $(this).focusout(function(){
				 if($(this).val()=='') $(this).val('Subject');
			 }); break;

		 }
	});

	$("#messages-cancel").click(function(){
		$("#add-messages-wrap").fadeOut("slow");
	});

	$("#messages-cancel").click(function(){
		$("#add-messages-wrap").fadeOut("slow");
		/* $('#m-to').val('To');
		$('#m-subject').val('Subject');

		return false; */

	});

	$("#messages-submit").click(function(){

		var to = $('#m-to').attr("value");
		var subject = $('#m-subject').attr("value");
		var message = $('#m-message').attr("value");
		var baseurl = $('#baseURL').val();
		var error = "";

		if(to == "To"){
			error = "Please fill up your Recipient<br>";
			$('#m-to').css("border","1px solid #FF0000");
		}
		else
		{
			$('#m-to').css("border","0px solid #FF0000");
		}

		if(subject == "Subject"){
			error = error+"Please fill up Subject<br>";
			$('#m-subject').css("border","1px solid #FF0000");
		}
		else{
			$('#m-subject').css("border","0px solid #FF0000");
		}
		if(message == ""){
			error = error+"Please fill up your Message<br>";
			$('#m-message').css("border","1px solid #FF0000");
		}
		else{
			$('#m-message').css("border","0px solid #FF0000");
		}

		if(error == ""){

			var form_data = $('#add_messages_frm').serialize();
			var path = $('#hidden2').attr("value");
			//alert(path);
			$.ajax({

				url: path,
				data: form_data,
				type : 'POST',

				 beforeSend: function(){
					$(".error-r").hide();
					$(".loader").show();
				 },

				 success: function(data){
				 	//alert (data);exit;
						$('.loader').fadeOut(3000,function(){
						$(".error-r").css('margin-top','0');
							if(data !='error' ){
								msgtype = $('#msgtype').val();
								if($('#file_uploadQueue').html() == '' || msgtype == 'notes' || msgtype == 'journal') {
									if(saveCaptured()){
										$('.error-r').html("Your message is successfully sent.").fadeIn('slow',function(){
											window.location = baseurl+'messages/type/'+msgtype;
											//window.location = baseurl+'messages/type/'+msgtype;
											//$("#add-messages-wrap").fadeOut('slow');
										});
									}
								} else {
									$('#file_upload').uploadifyUpload();
								}
								$('.message_menu .current').trigger('click');
								//location.href = baseurl+'messages/type/'+msgtype;
							}else {
								$('.error-r').html("Failed to create message.").fadeIn(500).delay(4000, function(){
									$("#add-messages-wrap").hide();
								});
							}
					 	});
				},

				 complete: function(data){

				}

			});
		}else{
			$('.error-r').html(error)
			$('.error-r').show();
		}
		return false;

	});

});

/*-------------------------------------
		save the captured images to database
	-------------------------------------*/



function saveCaptured(){
	var acount = $('#photos a').length;
	if(acount < 1) return true;

	var baseurl = $('#baseurl').val();
	var msgtype = $('#msgtype').val();
	var path = baseurl+'messages/uploadCaptured';

	var capturedA = [];

	$('#photos a').each(function(){
		capturedA.push($(this).attr('href'));
	});

	//alert(capturedA.length);
	for(var i=0; i<capturedA.length; i++){
		//alert(i);
		//alert(capturedA[i]);
	}
	var post_data = {'msgtype': msgtype, 'capturedA': capturedA};
	$.ajax({
		url: path,
		data: post_data,
		type : 'POST',

		beforeSend: function(){
			//$("#captured-loader").html('<span class="pending">Uploading Captured Photos...</span>');
			$('.error-r').html("Uploading Captured Photos...");
		},

		success: function(response){
			//alert(response)
		 	$('.error-r').html("Uploading Completed!").fadeIn(500).delay(1000).fadeOut(500, function(){
		 		window.location = baseurl+'messages/type/'+msgtype;
		 	});
		},

		complete: function(response){
			//alert(response)
			//$("#captured-loader").html('<span class="success">Uploading Completed!</span>');

		}

	});
	return true;
}

function toggleRecipWrap(){
		//$('div.absolute-recip').hide();
	}

function DeleteMessage(id){
	if( confirm("Do you really want to delete this message?") ){
		var baseurl = $('#baseurl').val();
		var url = baseurl+'messages/delete/'+id;
		$.ajax({
			url: url,
			type: 'POST',
			data: {},
			success: function(data) {
				$('#mssg'+id).remove();
			}
		});
	}
}

function recipDropdown(id){
	//alert('ok');
	$('#msg-recipient-wrap-'+id).toggle();

}
